package com.ncpowernode.javaweb.web;

import com.ncpowernode.javaweb.bean.Book;
import com.ncpowernode.javaweb.service.BookService;
import com.ncpowernode.javaweb.service.impl.BookServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

/**
 * 页面展示和业务的调用
 */
@WebServlet({"/book/list","/book/add","/book/delete","/book/modify","/book/save","/book/appraise"})
public class Controller extends HttpServlet {

    private BookService bookService = new BookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String s = request.getServletPath();
            if (s.equals("/book/list")) {
                bookService.list(request, response);
                request.getRequestDispatcher("/list.jsp").forward(request,response);
            } else if (s.equals("/book/add")) {
                bookService.add(request, response);
                response.sendRedirect(request.getContextPath() + "/book/list");
            } else if (s.equals("/book/delete")) {
                bookService.delete(request, response);
                response.sendRedirect(request.getContextPath() + "/book/list");
            } else if (s.equals("/book/modify")) {
                bookService.modify(request, response);
                request.getRequestDispatcher("/modify.jsp").forward(request,response);
            }else if(s.equals("/book/save")){
                bookService.save(request,response);
                response.sendRedirect(request.getContextPath() + "/book/list");
            }else if(s.equals("/book/appraise")){
                bookService.appraise(request,response);
            } else {
                response.sendRedirect(request.getContextPath() + "error.html");
            }
        }catch(Exception e){
            response.sendRedirect(request.getContextPath() + "error.html");
        }
    }
}
